package com.wsx.rpc.metrics.histograms;


import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author wangshuaixin
 * @Title: com.wsx.rpc.metrics.histograms.HistogramsTest
 * @Description: 统计数据的分布情况，最大值、最小值、平均值、中位数，百分比（75%、90%、95%、98%、99%和99.9%
 * @date 2018/11/27
 */
public class HistogramsTest {

    private static final MetricRegistry REGISTRY = new MetricRegistry();

    private static ConsoleReporter reporter = ConsoleReporter.forRegistry(REGISTRY).build();

    private static final Histogram HISTOGRAM = REGISTRY.histogram(MetricRegistry.name(HistogramsTest.class, "random"));


    public static void main(String[] args) {
        reporter.start(3, TimeUnit.SECONDS);

        Random random = new Random();
        int num = 1;
        boolean go = true;
        while (go) {
            num ++;
            handleRequest(random.nextDouble());
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (num >= 1000) {
                go = false;
            }
        }
    }


    private static void handleRequest(double random) {
        HISTOGRAM.update((int)(random * 100));
    }
}
